'''
Created on Jun 21, 2012

@author: thomas_dao
'''
from web import base
from web.base import BaseHandler
from web import urls
from web import util
from entity.entity import Bill, FriendBill
import json
import logging
from google.appengine.ext import ndb
from app import bill_async

     
class CreateHandler(BaseHandler):
    @ndb.synctasklet
    @base.user_required
    def post(self):
        """
        @param title: bill title
        @param message: message to used in email
        @param total_amount: bill total amount
        @param currency: should be symbol like $
        @param remind_freq: once a week, twice a week,...
        @param friend_bills: json array 
        [
            {
                "name":"thomas", 
                "email":"thomas@test.com", 
                "amount":'10', 
                "paid":"paid", 
                "detail":"Cake"
            }
        ]
        """
        # Get request parameters
        title = self.request.params.get('title')
        message = self.request.params.get('message')
        total_amount = self.request.params.get('total_amount')
        remind_freq = self.request.params.get('remind_freq')
        owner = util.get_current_user_key()
        friend_bills = self.request.params.get('friend_bills')
        logging.info(friend_bills)
        friend_bills = json.loads(friend_bills)
        
        # Create and save bills
        bill = Bill()
        bill.title = title
        bill.message = message
        bill.total_amount = float(total_amount)
        bill.remind_freq = remind_freq
        bill.owner = owner
        bill.put()
        
        yield bill_async.create_friend_bills(friend_bills=friend_bills, bill=bill)
        
        page = yield bill_async.my_created_bills(util.get_current_user_key())
        self.response.write(json.dumps(page))                

class ReadHandler(BaseHandler):
    def get(self, bill_id):
        bill = Bill.get_by_id(int(bill_id))
        if bill:
            self.response.write(json.dumps(util.ndb_to_dict(bill)))
    

class UpdateHandler(BaseHandler):
    @base.user_required
    def post(self):
        bill_id = self.request.params.get('bill_id')
        remind_freq = self.request.params.get('remind_freq')
        bill = Bill.get_by_id(int(bill_id))
        bill.remind_freq = remind_freq
        bill.put()
        self.response.write(json.dumps(util.ndb_to_dict(bill)))
    

class DeleteHandler(BaseHandler):
    @ndb.synctasklet
    @base.user_required
    def post(self):
        bill_id = self.request.params.get('bill_id')
        bill_key = ndb.Key(Bill, int(bill_id))
        qry = FriendBill.query(FriendBill.bill==bill_key)
        friend_bill_keys = yield qry.fetch_async(keys_only=True)
        
        yield ndb.delete_multi_async([bill_key] + friend_bill_keys)
        
        # Response
        page = yield bill_async.my_created_bills(util.get_current_user_key())
        self.response.write(json.dumps(page))


class CreatedBillsHandler(BaseHandler):
    @ndb.synctasklet
    @base.user_required
    def get(self):
        page = yield bill_async.my_created_bills(util.get_current_user_key())
        self.response.write(json.dumps(page))

class RequestedBillsHandler(BaseHandler):
    @ndb.synctasklet
    @base.user_required
    def get(self):
        page = yield bill_async.my_requested_bills(util.get_current_user_key())
        self.response.write(json.dumps(page))

    
app = urls.app

def main():
    app.run()

if __name__ == '__main__':
    main()